package com.anzhixin.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JOptionPane;

/**
 * 一个文件拷贝函数： fileCopy(String a ,String b) a--表示原文件名 b--表示目标文件名扩展： 如果a是文件，则copy
 * a到b ; 如果a是目录，则copy a下的所有文件和文件夹（包括子文件夹）到b目录下。
 */
public class FileUtil {
	public static final int FILE_SOURCE_NOT_EXIST = -1;
	public static final int FILE_SOURCE_CAN_NOT_READ = -2;
	public static final int FILE_DEST_CAN_NOT_WRITE = -3;
	public static final int OK = 0;

	public int fileCopy(String a, String b) {
		File file = new File(a);

		if (!file.exists()) {
			System.out.println(a + "  Not Exists. ");
			return FILE_SOURCE_NOT_EXIST;
		}
		File fileb = new File(b);

		if (file.isFile()) {
			FileInputStream fis = null;
			FileOutputStream fos = null;
			try {
				fis = new FileInputStream(file);
				fos = new FileOutputStream(fileb);

				byte[] bb = new byte[(int) file.length()];
				fis.read(bb);
				fos.write(bb);

			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return FILE_SOURCE_CAN_NOT_READ;
			} catch (IOException e) {
				e.printStackTrace();
				return FILE_SOURCE_CAN_NOT_READ;
			} finally {
				try {
					fis.close();
					fos.close();
				} catch (Exception e) {
					e.printStackTrace();
					return FILE_DEST_CAN_NOT_WRITE;
				}
			}
		} else if (file.isDirectory()) {
			if (!fileb.exists()) {
				fileb.mkdir();
			}
			String[] fileList;
			fileList = file.list();
			for (int i = 0; i < fileList.length; i++) {
				int retCode = fileCopy(a + "/" + fileList[i], b + "/" + fileList[i]);
				if(retCode!=0){
					return retCode;
				}
			}
		}
		return OK;

	}
	/**
	 * 解析字符串，以便得到一个可用的相对目录
	 * 已知目录
	 * 相对已知目录的相对路径，如../../../aa
	 * */
	public static String getRelativePath(String sourceStr, String relativeStr){
		File f = new File(sourceStr);
		if(relativeStr==null||relativeStr.equals("")||".".equals(relativeStr)||relativeStr.startsWith("./")){
			return sourceStr;
		}
		if(relativeStr.startsWith("..")){
			return getRelativePath(f.getParent(),relativeStr.substring(3));
		}else{
			return sourceStr + "/" + relativeStr;
		}
	}
	
	
	public static void main(String[] ar) {
		
		/*int done=0;
		while(done==0){
			int retCode=new FileUtil().fileCopy("E:\\MyData\\aaaa_10_mail_batch\\anzhixinMailSender\\update_files",
			"E:\\MyData\\aaaa_10_mail_batch\\anzhixinMailSender");
			if(retCode==0){
				done=1;
			}else{
				if(JOptionPane.NO_OPTION==JOptionPane.showConfirmDialog(null, "文件复制过程中出错，请关闭相关正在运行的程序后重试！点‘是’重试，点‘否’您可能下次需要重新升级！", "重试？", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE)){
					done=2;
				}
			}
		}*/
		
		System.out.println("1:"+Thread.currentThread().getContextClassLoader()
				.getResource(""));
				  System.out.println("2:"+FileUtil.class.getClassLoader().getResource(""));
				  System.out.println("3:"+ClassLoader.getSystemResource(""));
				  System.out.println("4:"+FileUtil.class.getResource(""));
				  System.out.println("5:"+FileUtil.class.getResource("/"));
				  // Class文件所在路径
				  System.out.println("6:"+new File("/").getAbsolutePath());
				  System.out.println("7:"+System.getProperty("user.dir"));
		System.out.println("../aabb".substring(2));
		
		System.out.println(FileUtil.getRelativePath("E:/aaaa/bbb/ccc/ddd/ffff/ggg", "../../eeee"));
		System.out.println(FileUtil.getRelativePath("E:/aaaa/bbb/ccc/ddd/ffff/ggg", "eeee/asas/dsfsd"));
		System.out.println(FileUtil.getRelativePath("E:/aaaa/bbb/ccc/ddd/ffff/ggg", ""));
		System.out.println(FileUtil.getRelativePath("E:/aaaa/bbb/ccc/ddd/ffff/ggg", "./"));
		System.out.println(FileUtil.getRelativePath("E:/aaaa/bbb/ccc/ddd/ffff/ggg", "."));
	}
}
